#coding=utf-8

"""
Author: Maple
"""

import os
import sys
import wmi
import wx

"""
Get Windows partition-list:
Return: A list of partition-instance.
"""
def get_partition_info():
    partition_list = []
    w = wmi.WMI()
    for physical_disk in w.Win32_DiskDrive():
        for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"):
            for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
                partition_list.append(logical_disk)
    return partition_list

"""
File index generator:
    dir_list -> Directory list
Return: A file index generator
"""
def build_file_index(dir_list):
    for dir_element in dir_list:
        if not dir_element.endswith(os.sep):
            dir_element = dir_element + os.sep
        walk_rs = os.walk(dir_element)
        for e in walk_rs:
            for f in e[2]:
                fpath = os.path.join(e[0], f)
                yield fpath

"""
File filter:
    file_list   -> List or Iterator
    text        -> Filter text
    mode        -> Match file or directory 
    match       -> Match method, starts, ends , full or in
Return: A file generator of filtered  
"""
def get_files(file_list=[], text='', mode='file', match='starts'):
    for fpath in file_list:
        if mode == 'dir':
            if text in fpath.split(os.sep)[:-1]:
                yield fpath
        if mode == 'file':
            if match in ['starts', 'ends']:
                code_str = '"%s".%s("%s")' % (fpath.split(os.sep)[-1], match+'with', text)
            if match == 'full':
                code_str = '"%s" == "%s"' % (text, fpath.split(os.sep)[-1])
            if match == 'in':
                code_str = '"%s" in "%s"' % (text, fpath.split(os.sep)[-1])
            if eval(code_str):
                yield fpath


